﻿using System;
using System.Threading;
using System.ComponentModel;
namespace OtherDevice
{
    [Description("XNC-双通道")]
    class LightCtrlXNC_Two : SpecialLightCtrlBase
    {
        public LightCtrlXNC_Two(string strName) : base(strName)
        {
            controlFrom = new Form_sino二色温();
        }
        public override bool CloseAll()
        {
            try
            {
                Byte[] bs = new byte[] { 0x55, 0xaa, 0x01, 0x05, 0x00, 0x00, 0x00, 0xfa };
                int sum = 0;
                for (int i = 0; i < 7; i++)
                {
                    sum += bs[i];
                }
                bs[7] = (byte)(sum ^ 0xff);
                byte[] byteRe = new byte[8];
                return SendAndReciveData(bs, ref byteRe, 500);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"光源设置失败{ex}");
                return false;
            }
        }
        public override bool Open(int channel)
        {
            try
            {

                Byte[] bs = new byte[] { 0x55, 0xaa, 0x01, 0x05, 0x01, 0x00, 0x00, 0xfa };
                bs[5] = (byte)channel;
                int sum = 0;
                for (int i = 0; i < 7; i++)
                {
                    sum += bs[i];
                }
                bs[7] = (byte)(sum ^ 0xff);
                byte[] byteRe = new byte[8];
                return SendAndReciveData(bs, ref byteRe, 500);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"光源设置失败{ex}");
                return false;
            }
        }
        public override int Get(int channel)
        {
            Byte[] bs = new byte[] { 0x55, 0xaa, 0x01, 0x02, 0x00, 0x00, 0x00, 0xfa };
            bs[4] = (byte)channel;
            int sum = 0;
            for (int i = 0; i < 7; i++)
            {
                sum += bs[i];
            }
            bs[7] = (byte)(sum ^ 0Xff);
            byte[] byteRe = new byte[8];
            if (SendAndReciveData(bs, ref byteRe, 500))
                return byteRe[6] + byteRe[5] * 256;
            else
                return -999;

        }
        public override bool Set(int channel, int value)
        {
            try
            {

                Byte[] bs = new byte[] { 0x55, 0xaa, 0x01, 0x02, 0x00, 0x00, 0x00, 0xfa };
                bs[4] = (byte)channel;
                int t = value / 256;
                int t2 = value % 256;
                bs[5] = Convert.ToByte(t2);
                bs[6] = Convert.ToByte(t);
                int sum = 0;
                for (int i = 0; i < 7; i++)
                {
                    sum += bs[i];
                }
                bs[7] = (byte)(sum ^ 0xff);

                byte[] byteRe = new byte[8];
                return SendAndReciveData(bs, ref byteRe, 500);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"光源设置失败{ex}");
                return false;
            }
        }
    }
}
